<?php
namespace app\mapi\model;
use think\Model;

/**
 * 商品收藏模型类
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallHouseModel extends Model
{

    protected $name = 'MallHouse';

    /**
     * 数据列表方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @param   Int     $Pages  分页页数
     * @param   Int     $Limit  每页条数
     * @param   Srting  $Order  查询排序
     * @return  Array
     */
    public function mList($Where = [], $Field = 'house_goods,house_users,goods_name,goods_img', $Pages = 0, $Limit = 10, $Order = 'house_create DESC'){
        $Lists = $this->field($Field)->join('db_mall_goods', 'db_mall_house.house_goods = db_mall_goods.goods_id', 'LEFT')->where($Where)->order($Order)->limit($Pages * $Limit, $Limit)->select();

        return ObjectToArray($Lists);
    }

    /**
     * 数据统计方法
     * @param   Array   $Where  查询条件
     * @return  Int
     */
    public function mCount($Where = []){
        return $this->where($Where)->count();
    }

    /**
     * 数据详情方法
     * @param   Array   $Where  查询条件
     * @param   String  $Field  查询字段
     * @return  Array
     */
    public function mInfo($Where = [], $Field = '*'){
        $Infos = $this->field($Field)->where($Where)->find();

        return ObjectToArray($Infos);
    }

    /**
     * 数据新增方法
     * @param   Array   $Param  添加内容
     * @return  Array
     */
    public function mAdd($Param){
        $Code = 2;
        $Time = time();

        try{
            $Info = $this->mInfo($Param);

            if(empty($Info)){
                $Param['house_create'] = $Time;

                $Result = $this->allowField(true)->insert($Param);

                if($Result){
                    $Code = 1;
                    $Msgs = '收藏成功';
                }else{
                    $Code = 2;
                    $Msgs = '收藏失败';
                }
            }else{
                $Code = 2;
                $Msgs = '已收藏过';
            }

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据删除方法
     * @param   Array   $Where  更新条件
     * @return  Array
     */
    public function mDel($Where){
        $Code = 2;
        $Time = time();

        try{
            $Result = $this->allowField(true)->where($Where)->delete();

            if($Result){
                $Code = 1;
                $Msgs = '删除成功';
            }else{
                $Msgs = '删除失败';
            }

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

}